package com.hudilao.task;


import com.hudilao.entity.Order;
import com.hudilao.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Component
public class OrderExpirationTask {
    @Autowired
    private OrderMapper orderMapper;


    /**
     * 定时清理已提交而未支付的订单
     */
    @Scheduled(cron = "0 0/1 * * * ?")          //每分钟执行一次
    public void processTimeoutOrder(){
        LocalDateTime time = LocalDateTime.now().plusMinutes(-15);
        List<Order> orderList = orderMapper.getByStatusAndCreateTimeLessThan(Order.SUBMITTED, time);

        if(orderList != null && orderList.size() > 0){
            for (Order order : orderList) {
                order.setStatus(Order.CANCELLED);
                orderMapper.updateById(order);
            }
        }
    }
}
